<template>
  <div class="movie">
    <div
      class="item"
      v-for="item in movieList"
      :key="item._id"
      @click="goToDetail(item.movieid)"
    >
      <img :src="item.img | format" alt="" />
      <div class="RightBox">
        <div class="text">
          <h5>
            {{ item.nm }}
            <img
              src="../../assets/images/2dimax.png"
              alt=""
              v-if="item.version == 'v2d imax'"
            />
            <img
              src="../../assets/images/v3d.png"
              alt=""
              v-else-if="item.version == 'v3d imax'"
              style="width: 17px"
            />
          </h5>
          <div class="remark">
            <p v-if="item.sc">
              观众评
              <span>{{ item.sc }}</span>
            </p>
            <p v-else-if="item.movieid">
              <span>{{ item.movieid }}</span>
              人想看
            </p>
            <p v-else>暂无评分</p>
          </div>
          <p>主演:{{ item.star }}</p>
          <p>{{ item.showInfo }}</p>
        </div>
        <div class="button">
          <h2 v-if="item.showst == 3">购票</h2>
          <h2 v-else style="background: rgb(60, 159, 230)">预售</h2>
        </div>
      </div>
    </div>
    <p v-if="movieList.length >= count && count">没有更多了</p>
  </div>
</template>

<script>
import { moviesApi, moviesMoreApi } from "../../utils/api";
export default {
  // props: {
  //   num: Number,
  // },
  data() {
    return {
      movieList: [],
      ids: [],
      count: 0,
      // 每次要请求多少条
      size: 6,
      // 从哪一条开始截取
      startIndex: 0,
    };
  },
  // watch: {
  //   num() {
  //     console.log("num");
  //   },
  // },
  mounted() {
    this.getData();
  },

  methods: {
    async getData() {
      const res = await moviesApi();
      this.movieList = res.result;
      // 获得ids和总条数
      this.ids = res.ids;
      this.count = res.count;
      this.startIndex = res.result.length;

      await this.$nextTick();

      // 触发父组件事件
      // 请求结束了，告知父组件去添加bs
      this.$emit("addbs");
    },

    async getMore() {
      // 去请求加载更多的数据接口
      // console.log("getMore");
      const ids = this.ids.slice(this.startIndex, this.startIndex + this.size);
      // console.log(ids);
      const str = ids.join(",");

      const res = await moviesMoreApi({
        ids: str,
      });
      // 将拿到的数据拼接到原数据上面
      this.movieList = [...this.movieList, ...res.result];
      this.startIndex += this.size;

      await this.$nextTick();
      this.$emit("finish", this.movieList.length >= this.count);
    },

    goToDetail(id) {
      this.$router.push("/detail/" + id);
    },
  },

  filters: {
    format(val) {
      // let arr = val.split("/");
      // arr[3] = "64.90";
      // let str = arr.join("/");
      // return str;
      return val.split("/w.h").join("/64.90");
    },
  },
};
</script>

<style lang="less" scoped>
@import "../../assets/css/var.less";
.movie {
  width: 100%;
  margin-top: 15px;
  background-color: #fff;

  .item {
    width: 100%;
    display: flex;
    padding-left: 15px;

    img {
      width: 64px;
      height: 86px;
      margin-top: 12px;
    }

    .RightBox {
      width: 301px;
      border-bottom: 1px solid #eee;
      padding: 11px 14px 12px 0;
      margin-left: 12px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .text {
        h5 {
          width: 180px;
          margin-top: 4px;
          font-size: 17px;
          .ell();

          img {
            width: 43px;
            height: 14px;
            margin: 5px 3px 0 0;
          }
        }

        .remark {
          p {
            margin-top: 8px;
            font-size: @xs-font;

            span {
              font-size: @s-font;
              color: @score-color;
              font-weight: 700;
            }
          }
        }

        p:nth-of-type(1) {
          margin-top: 6px;
          font-size: @xs-font;
          width: 200px;
          .ell();
        }

        p:nth-of-type(2) {
          margin-top: 6px;
          font-size: @xs-font;
        }
      }

      .button {
        h2 {
          width: 54px;
          height: 28px;
          background-color: @theme-color;
          border-radius: 24px;
          font-size: @xs-font;
          color: #fff;
          .center();
          font-weight: 500;
        }
      }
    }
  }
}
</style>
